<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>编辑订单</title>
    <link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@500&display=swap" rel="stylesheet">
    <style>
        body {
            background: #f8f1f4;
            font-family: 'Orbitron', sans-serif;
            color: #333;
        }
        .card {
            max-width: 600px;
            margin: 60px auto;
            padding: 30px;
            background: linear-gradient(145deg, #ffffff, #f5e8ee);
            border: 2px solid #ff6b9d;
            border-radius: 12px;
            box-shadow: 0 0 20px #ff6b9d;
        }
        h2 {
            text-align: center;
            color: #ff6b9d;
            text-shadow: 0 0 5px #ff6b9d;
            margin-bottom: 25px;
        }
        label {
            display: block;
            margin-top: 15px;
        }
        input, select, textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ff6b9d;
            background: #fff5f8;
            color: #333;
            border-radius: 6px;
            font-size: 14px;
        }
        .btn-group {
            text-align: center;
            margin-top: 20px;
        }
        .btn {
            background: transparent;
            border: 2px solid #ff6b9d;
            color: #ff6b9d;
            padding: 10px 25px;
            margin: 0 10px;
            border-radius: 6px;
            cursor: pointer;
            transition: 0.3s;
        }
        .btn:hover {
            background: #ff6b9d;
            color: #fff;
        }
    </style>
    <script>
        function validateForm() {
            const createdInput = document.forms["orderForm"]["created"];
            const createdValue = createdInput.value;

            if (!createdValue) {
                alert("请选择创建时间");
                return false;
            }

            // 检查格式是否为 yyyy-MM-dd
            const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
            if (!dateRegex.test(createdValue)) {
                alert("日期格式必须为 yyyy-MM-dd");
                return false;
            }

            // 检查年份不超过2025
            const year = parseInt(createdValue.split('-')[0], 10);
            if (year > 2025) {
                alert("年份不能大于2025");
                return false;
            }

            // 检查月份和日期有效性
            const month = parseInt(createdValue.split('-')[1], 10);
            const day = parseInt(createdValue.split('-')[2], 10);
            if (month < 1 || month > 12) {
                alert("月份必须在1-12之间");
                return false;
            }
            if (day < 1 || day > 31) {
                alert("日期无效");
                return false;
            }

            return true;
        }
    </script>
</head>
<body>
<div class="card">
    <h2>编辑订单</h2>
    <form name="orderForm" action="${pageContext.request.contextPath}/order/update" method="post" onsubmit="return validateForm();">
        <input type="hidden" name="oid" value="${order.oid}" />

        <label>用户</label>
        <select name="uid" required>
            <c:forEach var="user" items="${userList}">
                <option value="${user.uid}" ${user.uid == order.uid ? 'selected' : ''}>${user.uname}</option>
            </c:forEach>
        </select>

        <label>总金额</label>
        <input type="number" step="0.01" name="total" value="${order.total}" required>

        <label>订单状态</label>
        <select name="status" required>
            <c:forEach var="s" items="${['待支付','已支付','已发货','已完成','已取消']}">
                <option value="${s}" ${s == order.status ? 'selected' : ''}>${s}</option>
            </c:forEach>
        </select>

        <label>收货信息</label>
        <textarea name="receiveInfo" rows="3">${order.receiveInfo}</textarea>

        <label>创建时间</label>
        <input type="date" name="created"
               value="<fmt:formatDate value='${order.created}' pattern='yyyy-MM-dd'/>"
               required>

        <div class="btn-group">
            <input type="submit" class="btn" value="更新">
            <input type="button" class="btn" value="取消" onclick="window.location.href='${pageContext.request.contextPath}/order/list'">
        </div>
    </form>
</div>
</body>
</html>